package com.rong.action;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.text.DateFormat;
import java.util.Date;
import org.hibernate.Query;
import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.rong.DAO.ZynBusiInputDAO;
import com.rong.DAO.ZynSellDAO;
import com.rong.DAO.ZynTransferDAO;
import com.rong.DAO.ZynUserRightDAO;
import com.rong.DAO.ZynStatusDAO;
import com.rong.ORM.ZynBusiInput;
import com.rong.ORM.ZynSell;
import com.rong.ORM.ZynTransfer;
import com.rong.ORM.ZynUser;
import com.rong.ORM.ZynUserRight;
import com.rong.hibernate.BaseHibernateDAO;
import com.rong.ORM.ZynQueryCriter;


public class RepAction extends ActionSupport {
	
	ZynBusiInput busi=new ZynBusiInput();
	ZynBusiInputDAO busiDao=new ZynBusiInputDAO();
	ZynUserRightDAO userrightDao= new ZynUserRightDAO();
	ZynStatusDAO statusDao=new ZynStatusDAO();
	private List<ZynBusiInput> list;
	private String saveFlag="";
	private int id=-1; // the busi id indatabase.
	
	private List<ZynTransfer> transfList;
	private List<ZynSell> sellList;
	ZynTransferDAO traferDao=new ZynTransferDAO();
	ZynSellDAO sellDao=new ZynSellDAO();
	private String[] extAttr3_status;  //单据的状态 ，复核，核销，签批等等
	ZynUserRight userright = new ZynUserRight();
	String op="";
	
	
	private int rowsPerPage =25;// 每页显示几条
	private int page = 1; // 默认当前页
	private int totalPage;// 总共多少页
	private int planNum;// 总过多少条
	
	ZynQueryCriter queryCrt=new ZynQueryCriter();
	/**
	 * @return the rowsPerPage
	 */
	public int getRowsPerPage() {
		return rowsPerPage;
	}
	/**
	 * @param rowsPerPage the rowsPerPage to set
	 */
	public void setRowsPerPage(int rowsPerPage) {
		this.rowsPerPage = rowsPerPage;
	}
	/**
	 * @return the page
	 */
	public int getPage() {
		return page;
	}
	/**
	 * @param page the page to set
	 */
	public void setPage(int page) {
		this.page = page;
	}
	/**
	 * @return the totalPage
	 */
	public int getTotalPage() {
		return totalPage;
	}
	/**
	 * @param totalPage the totalPage to set
	 */
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	/**
	 * @return the planNum
	 */
	public int getPlanNum() {
		return planNum;
	}
	/**
	 * @param planNum the planNum to set
	 */
	public void setPlanNum(int planNum) {
		this.planNum = planNum;
	}
	/**
	 * @return the op
	 */
	public String getOp() {
		return op;
	}
	/**
	 * @param op the op to set
	 */
	public void setOp(String op) {
		this.op = op;
	}
	/**
	 * @return the extAttr3_status
	 */
	public String[] getExtAttr3_status() {
		return extAttr3_status;
	}
	/**
	 * @param extAttr3_status the extAttr3_status to set
	 */
	public void setExtAttr3_status(String[] extAttr3_status) {
		this.extAttr3_status = extAttr3_status;
	}
	/**
	 * @return the transfList
	 */
	public List<ZynTransfer> getTransfList() {
		return transfList;
	}
	/**
	 * @param transfList the transfList to set
	 */
	public void setTransfList(List<ZynTransfer> transfList) {
		this.transfList = transfList;
	}
	/**
	 * @return the sellList
	 */
	public List<ZynSell> getSellList() {
		return sellList;
	}
	/**
	 * @param sellList the sellList to set
	 */
	public void setSellList(List<ZynSell> sellList) {
		this.sellList = sellList;
	}
	/**
	 * @return the busi
	 */
	public ZynBusiInput getBusi() {
		return busi;
	}
	/**
	 * @param busi the busi to set
	 */
	public void setBusi(ZynBusiInput busi) {
		this.busi = busi;
	}
	/**
	 * @return the list
	 */
	public List<ZynBusiInput> getList() {
		return list;
	}
	/**
	 * @param list the list to set
	 */
	public void setList(List<ZynBusiInput> list) {
		this.list = list;
	}
	/**
	 * @return the saveFlag
	 */
	public String getSaveFlag() {
		return saveFlag;
	}
	/**
	 * @param saveFlag the saveFlag to set
	 */
	public void setSaveFlag(String saveFlag) {
		this.saveFlag = saveFlag;
	}
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	
	
	/**
	 * @return the queryCrt
	 */
	public ZynQueryCriter getQueryCrt() {
		return queryCrt;
	}
	/**
	 * @param queryCrt the queryCrt to set
	 */
	public void setQueryCrt(ZynQueryCriter queryCrt) {
		this.queryCrt = queryCrt;
	}
	/**
	 * @return the userright
	 */
	public ZynUserRight getUserright() {
		return userright;
	}
	/**
	 * @param userright the userright to set
	 */
	public void setUserright(ZynUserRight userright) {
		this.userright = userright;
	}
	@Override
    public String execute() throws Exception {
		
		
		
		
//				        this.setSaveFlag("");
//				        DateFormat format2=new java.text.SimpleDateFormat("yyyyMMdd");
//				        int s=busiDao.findByHeatDate(this.busi.getHeatDate()).size()+1;
//				        String n=format2.format(this.busi.getHeatDate())+s;
//				        this.busi.setNum(n);
//						busiDao.save(this.busi);        //保存注册的busi对象
//						//保存运费和销售费用LIST
//						if(transfList!=null &&!transfList.equals("")){
//							for(Iterator<ZynTransfer> iterTra=transfList.iterator();iterTra.hasNext();){
//								ZynTransfer trfer=(ZynTransfer)iterTra.next();
//								if(trfer.getTranNum().length()==0){continue;}// if transfer num is null ,this is no record ,do not save this record,				
//								traferDao.save(trfer);
//							}			
//						}
//						if(sellList!=null && !sellList.equals("")){
//							for(Iterator<ZynSell> iterSell=sellList.iterator();iterSell.hasNext();){
//								ZynSell sell=(ZynSell)iterSell.next();
//								if(sell.getSellNum().length()==0){continue;} // the same with upper 
//								sellDao.save(sell);				
//							}			
//						}
//						
//						//保存运费和销售费用LIST end
//						 this.setSaveFlag("保存成功..");
		
		System.out.println("RepAction中的execte()...");
        return SUCCESS;
    }
	 public String findall() throws Exception {
	    
		 this.setList(busiDao.findAll());        //保存注册的user对象
	        return "success_list";
	    }
	 public String findbyid() throws Exception {
	        
		this.setBusi(busiDao.findById(this.getId()));// find the busi by id	
	        return "s_find_one";
	       
	    }
	 public String edit() throws Exception {
	      			
		   busiDao.merge(this.getBusi());// edit the busi by id			
		    return "s_edit_one";
		    }
	 public String delete() throws Exception {
	        
			busiDao.delete(busiDao.findById(this.getId()));// delete the busi by id			
		    return "s_delete_one";
		    }
	 public String  giveHeatList() throws Exception {		    
		 
		 //this.setSellList(sellDao.findAllAgain());        //日供热明细表	 
         
          this.setSellList(sellDao.findZynSelltByPage(page, rowsPerPage,this.getQueryCrt()));
         
         this.setPlanNum(sellDao.tatolRows(this.getQueryCrt()).size());
         this.setTotalPage(this.getPlanNum() % this.getRowsPerPage()==0?this.getPlanNum()/this.getRowsPerPage():this.getPlanNum()/this.getRowsPerPage()+1);
        
         ActionContext actionContext = ActionContext.getContext();         
         Map session = actionContext.getSession(); 
         ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户		 
		 List<ZynUserRight> rlist= userrightDao.findByUserId(loginUser.getId()); //权限的控制
		 for(int k=0;k<rlist.size();k++){
			 ZynUserRight tempUt=rlist.get(k);
			 if(tempUt.getMenu().getMenuName().equalsIgnoreCase("RepAction!giveHeatList.action")){
				 this.setUserright(tempUt);
			 }
		 }				 
	        return "gh_daily_list";
	    }
	 
	 /**
	  * 运输明细
	  * **/
 public String  repTransfList() throws Exception {		    
		         
         this.setTransfList(traferDao.findZynTransfertByPage(page, rowsPerPage,this.getQueryCrt()));        
         this.setPlanNum(traferDao.tatolRows(this.getQueryCrt()).size());
         this.setTotalPage(this.getPlanNum() % this.getRowsPerPage()==0?this.getPlanNum()/this.getRowsPerPage():this.getPlanNum()/this.getRowsPerPage()+1);
        
         ActionContext actionContext = ActionContext.getContext();         
         Map session = actionContext.getSession(); 
         ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户		 
		 List<ZynUserRight> rlist= userrightDao.findByUserId(loginUser.getId()); //权限的控制
		 for(int k=0;k<rlist.size();k++){
			 ZynUserRight tempUt=rlist.get(k);
			 if(tempUt.getMenu().getMenuName().equalsIgnoreCase("RepAction!repTransfList.action")){
				 this.setUserright(tempUt);
			 }
		 }				 
	        return "transf_daily_list";
	    }
 /***
  * 采热列表
  * **/
 public String  toGetHeatList() throws Exception {		    
	 
	
      this.setList(busiDao.findZynBusiInputtByPage(page, rowsPerPage,this.getQueryCrt()));
     
     this.setPlanNum(busiDao.tatolRows(this.getQueryCrt()).size());
     this.setTotalPage(this.getPlanNum() % this.getRowsPerPage()==0?this.getPlanNum()/this.getRowsPerPage():this.getPlanNum()/this.getRowsPerPage()+1);
    
     ActionContext actionContext = ActionContext.getContext();         
     Map session = actionContext.getSession(); 
     ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户		 
	 List<ZynUserRight> rlist= userrightDao.findByUserId(loginUser.getId()); //权限的控制
	 for(int k=0;k<rlist.size();k++){
		 ZynUserRight tempUt=rlist.get(k);
		 if(tempUt.getMenu().getMenuName().equalsIgnoreCase("RepAction!toGetHeatList.action")){
			 this.setUserright(tempUt);
		 }
	 }				 
        return "get_h_daily_list";
    }
 
 /***
  * 采水列表
  * **/
 public String  toGetWaterList() throws Exception {		    
	 
	
      this.setList(busiDao.findZynBusiWatertByPage(page, rowsPerPage,this.getQueryCrt()));  //采水分页
     
     this.setPlanNum(busiDao.WatertatolRows(this.getQueryCrt()).size());
     this.setTotalPage(this.getPlanNum() % this.getRowsPerPage()==0?this.getPlanNum()/this.getRowsPerPage():this.getPlanNum()/this.getRowsPerPage()+1);
    
     ActionContext actionContext = ActionContext.getContext();         
     Map session = actionContext.getSession(); 
     ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户		 
	 List<ZynUserRight> rlist= userrightDao.findByUserId(loginUser.getId()); //权限的控制
	 for(int k=0;k<rlist.size();k++){
		 ZynUserRight tempUt=rlist.get(k);
		 if(tempUt.getMenu().getMenuName().equalsIgnoreCase("RepAction!toGetWaterList.action")){
			 this.setUserright(tempUt);
		 }
	 }				 
        return "get_water_daily_list";
    }
 
 /**
  * 
  * 供热记录 更新状态
  * 
  * ***/
	 public String  updateApprov() throws Exception {//状态
		 Integer key;
		 for(int k=0;k<this.extAttr3_status.length;k++){
			 key = Integer.parseInt(this.extAttr3_status[k]) ;			 
			 ZynSell sellObj=sellDao.findById(key);			 
			 sellObj.setExtattr3(Integer.parseInt(this.getOp()));	
			
			 			 
			 if(this.getOp().equalsIgnoreCase("30")||this.getOp().equalsIgnoreCase("70")){ //如果为核销操作，记录正常核销还是异常核销历史
				 sellObj.setExtattrint5(Integer.parseInt(this.getOp()))  ; 
				 sellObj.setExtattrdate1(new Date());  //状态更新日期，核销后记录实际收款日期
			 }
			 
			 sellDao.merge(sellObj);
			System.out.println("=========update====3=====id:"+sellObj.getId()+"   ;status="+sellObj.getSellSts().getId()+" desc:"+sellObj.getSellSts().getName());	
			// System.out.println(k+"[]*************************: "+this.extAttr3_status[k]);
		 }
	        return this.giveHeatList();
	    }
	 /**
	  * 
	  * 运输明细记录 更新状态
	  * 
	  * ***/
	 public String  updateApprovTransf() throws Exception {//状态
		 Integer key;
		 for(int k=0;k<this.extAttr3_status.length;k++){
			 key = Integer.parseInt(this.extAttr3_status[k]) ;			 
			 ZynTransfer trfObj=traferDao.findById(key);			 
			 trfObj.setExtattr3(Integer.parseInt(this.getOp()));	
			 
			 
			 if(this.getOp().equalsIgnoreCase("30")||this.getOp().equalsIgnoreCase("70")){ //如果为核销操作，记录正常核销还是异常核销历史
				 trfObj.setExtattrint5(Integer.parseInt(this.getOp()))  ; 
				 trfObj.setExtattrdate1(new Date());  //状态更新日期，核销后记录付款日期
			 }
			 
			 traferDao.merge(trfObj);
			System.out.println("=======updateApprovTransf=======id:"+trfObj.getId()+"   ;status="+trfObj.getSellSts().getId()+" desc:"+trfObj.getSellSts().getName());	
			// System.out.println(k+"[]*************************: "+this.extAttr3_status[k]);
		 }
	        return this.repTransfList();
	    }
	 
	 /**
	  * 
	  * 采热记录 更新状态
	  * 
	  * ***/
	 public String  updateApprovGetHeat() throws Exception {//状态
		 Integer key;
		 for(int k=0;k<this.extAttr3_status.length;k++){
			 key = Integer.parseInt(this.extAttr3_status[k]) ;			 
			 ZynBusiInput ghObj=busiDao.findById(key);			 
			 ghObj.setExtattrint1(Integer.parseInt(this.getOp()));	//采热状态（复核，核销等）
			
			 
			 if(this.getOp().equalsIgnoreCase("30")||this.getOp().equalsIgnoreCase("70")){ //如果为核销操作，记录正常核销还是异常核销历史
				 ghObj.setExtattrint5(Integer.parseInt(this.getOp()))  ; 
				 ghObj.setExtattrdate1(new Date());  //状态更新日期，核销后记录实际付款日期
			 }
			 
			 busiDao.merge(ghObj);
			System.out.println("=======get heat update=======id:"+ghObj.getId()+"   ;extint1="+ghObj.getExtattrint1()+"   ;status="+ghObj.getSellSts().getId()+" desc:"+ghObj.getSellSts().getName());	
			// System.out.println(k+"[]*************************: "+this.extAttr3_status[k]);
		 }
	        return this.toGetHeatList();
	    }
	 
	 /**
	  * 
	  * 采水记录 更新状态
	  * 
	  * ***/
	 public String  updateApprovGetWater() throws Exception {//状态
		 Integer key;
		 for(int k=0;k<this.extAttr3_status.length;k++){
			 key = Integer.parseInt(this.extAttr3_status[k]) ;			 
			 ZynBusiInput ghObj=busiDao.findById(key);			 
			 ghObj.setExtattrint2(Integer.parseInt(this.getOp()));	//采水状态（复核，核销等）
			
			 
			 if(this.getOp().equalsIgnoreCase("30")||this.getOp().equalsIgnoreCase("70")){ //如果为核销操作，记录正常核销还是异常核销历史
				 ghObj.setExtattrint6(Integer.parseInt(this.getOp()))  ; 
				 ghObj.setExtattrdate2(new Date()); //状态更新日期，核销后记录实际付款日期
			 }
			 
			 busiDao.merge(ghObj);
			System.out.println("=======updateApprovGetWater=======id:"+ghObj.getId()+"   ;status="+ghObj.getExtattrint2()+" desc:"+ghObj.getSellStswater().getName());	
			// System.out.println(k+"[]*************************: "+this.extAttr3_status[k]);
		 }
	        return this.toGetWaterList();
	    }
	 
}
